package com.weishi.tcp;

import cn.hutool.json.JSONUtil;
import com.weishi.common.utils.DateUtils;
import com.weishi.common.utils.GPSUtil;
import com.weishi.common.utils.StringUtils;
import com.weishi.project.speedometer.vo.MqttDataVo;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AnalysisResults {

    public static void main(String[] args) throws Exception {
        //readFileToResult();
        readFileByLog();
    }

    /**
     * 日志文件恢复入库
     *
     * @throws Exception
     */
    private static void readFileByLog() throws Exception {
        String ss = "[MqttDataVo(devId=AHWS02, timeTimp=0.27s:-0.97:-0.20, type=wit_acc, datas=[AHWS02, F6:05:D9:5D:EC:9C, 0.27s:-0.97:-0.20, 62.99:62.99:-16.97, -106.50:-36.58:-3.35])]\n";
        File log = new File("D:\\temp\\sys-info.log");
        BufferedReader bReader = new BufferedReader(new FileReader(log));
        String strLine = null;
        List<String> list = new ArrayList<>();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
//        SimpleDateFormat df1 = new SimpleDateFormat("HHmmss.SSS");
        Double firstlat = null;
        Double firstlong = null;
        try {
            while ((strLine = bReader.readLine()) != null) {
                /*if (!strLine.contains("type=wit_acc")) {
                    continue;
                } *//*else if (!strLine.contains("F6:05:D9:5D:EC:9C")) {
                    continue;
                }*/ /*else if (!strLine.contains("AH01")) {
                    continue;
                }*/
                String strings[] = strLine.split(",");
                Date day = df1.parse("2021-09-17 "+strings[0]);
                long timp = day.getTime();
                String ctime = df.format(day);
                String dev_id = strings[2];
                int channel_id = 14502;

                String a[] = strings[3].split(":");
                double ax =Double.parseDouble(a[0]);
                double ay =Double.parseDouble(a[1]);
                double az =Double.parseDouble(a[2]);

                String w[] = strings[3].split(":");
                double wx =Double.parseDouble(w[0]);
                double wy =Double.parseDouble(w[1]);
                double wz =Double.parseDouble(w[2]);

                String angle[] = strings[3].split(":");
                double anglex =Double.parseDouble(angle[0]);
                double angley =Double.parseDouble(angle[1]);
                double anglez =Double.parseDouble(angle[2]);

                /*double distanceTemp = GPSUtil.GetDistance1(firstlat, firstlong, latitude, longitude);*/

//                String sql = "insert into tbl_ship_speed_record_log (ship_speed_sn,timp,longitude,latitude,speed,ctime,utc,distance,channel_id) values(\"" + arr[0] + "\"," + arr[1] + "," + arr[2] + "," + arr[3] + "," + arr[4] + ",\"" + time + "\"," + utc + "," + distanceTemp + "," + "23385);";
                String sql = "INSERT INTO `tbl_wit_acc_log` (`dev_id`, `channel_id`, `timp`, `ctime`, `ax`, `ay`, `az`, `wx`, `wy`, `wz`, `anglex`, `angley`, `anglez`) " +
                        "VALUES ('"+ dev_id+"', "+channel_id+",  "+timp+", '"+ctime+"', "+ax+", "+ay+", "+az+", "+wx+", "+wy+", "+wz+", "+anglex+", "+angley+", "+anglez+");";
                System.out.println(sql);
            }
            bReader.close();
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
    }

    private static void readFile() throws Exception {
        File log = new File("D:\\temp\\sys-info.log");
        File result = new File("D:\\temp\\result.csv");
        BufferedReader bReader = new BufferedReader(new FileReader(log));
        BufferedWriter bWriter = new BufferedWriter(new FileWriter(result));
        String strLine = null;
        List<String> list = new ArrayList<>();

        int i = 0;
        boolean writeFlag = false;

        while ((strLine = bReader.readLine()) != null) {
            if (!strLine.contains("TcpMassageDataVo")) {
                continue;
            }
            String[] strings = strLine.split(",");
            String from = strings[1];
            String timp = strings[2].split("=")[1];
            String deviceId = strings[3];
            if (deviceId.contains("AWWS03")) {

                String zcode = from.split(":")[2];

                bWriter.flush();
                bWriter.write(zcode + "," + DateUtils.getStringTime(Long.parseLong(timp)) + "," + deviceId.split("=")[1]);
                // 记得换行
                bWriter.newLine();
                i++;
            }

        }
        bReader.close();
        bWriter.close();
    }


    private static void readFile1() throws Exception {
        File log = new File("D:\\temp\\aa.txt");
        File result = new File("D:\\temp\\result.csv");
        BufferedReader bReader = new BufferedReader(new FileReader(log));
        BufferedWriter bWriter = new BufferedWriter(new FileWriter(result));
        String strLine = null;
        List<String> list = new ArrayList<>();

        int i = 0;
        boolean writeFlag = false;

        while ((strLine = bReader.readLine()) != null) {
            //strLine = strLine.replace("[INFO][Thread-6][TCPServercallBack.java:58]","");
            String[] strings = strLine.split(",");
            String copy = strings[3];

            /*if (list.contains(copy)) {
                continue;
            }
            list.add(copy);*/

            String zcode = strings[2];
            double z = Double.parseDouble(zcode);

           /* if(z<4d && z>-4d){
                continue;
            }*/

            Date date = new Date(Long.parseLong(copy));
            copy = DateUtils.parseDateToStr("HHmmss.S", date);
            double zMax = 0.0;
            if (!list.contains(copy)) {
                if (writeFlag) {
                    bWriter.flush();
                    bWriter.write(strings[0] + "," + strings[1] + "," + strings[2] + "," + zMax);
                    // 记得换行
                    bWriter.newLine();
                    i++;
                    writeFlag = false;
                } else {
                    writeFlag = true;
                }
                list.add(copy);
                zMax = z;
                continue;
            } else {
                zMax = Math.abs(zMax) > Math.abs(z) ? zMax : z;
            }
        }
        bReader.close();
        bWriter.close();
    }

    private static void readFileToResult() throws Exception {
        File log = new File("D:\\temp\\sys-info.log");
//        File log1 = new File("D:\\temp\\sys-info1.log");
        File result = new File("D:\\temp\\result.csv");
        BufferedReader bReader = new BufferedReader(new FileReader(log));
        BufferedWriter bWriter = new BufferedWriter(new FileWriter(result));

        String strLine = null;
        Double lat1 = null;
        Double lng1 = null;
        Double time1 = null;
        Double latbe = null;
        Double lngbe = null;
        Double timebe = null;


        Double total = 0.0;


        //表头数据
        String tile = "时间," + "维度," + "精度," + "与第一点距离," + "与上一点距离," + "与第一点时间," + "与上一点时间," + "速度," + "总距离";
        bWriter.write(tile);
        bWriter.newLine();

        while ((strLine = bReader.readLine()) != null) {
            if (strLine.contains("P120010919") && strLine.contains("gps")) {
              /*  bWriter.write(strLine);
                bWriter.newLine();*/
                String[] sss = strLine.split(",");
                //每秒取一个值
                /*if(!sss[3].contains(".00")){
                    continue;
                }*/
                String utc = "";
                double time = 10;
                String[] from = sss[2].replaceAll("\"", "").split(":");

                Double lat = Double.parseDouble(from[2]);
                Double lng = Double.parseDouble(from[1]);

                if (lat1 == null) {
                    lat1 = lat;
                    lng1 = lng;
                    latbe = lat;
                    lngbe = lng;
                    time1 = time;
                    timebe = time;
                    continue;
                }

                double mm1 = GPSUtil.GetDistance1(lat1, lng1, lat, lng);
                double mmbe = GPSUtil.GetDistance1(latbe, lngbe, lat, lng);
                /*if(time - timebe == 0){
                    continue;
                }*/
                String speed = from[3];

                total += mmbe;

                bWriter.flush();
                bWriter.write(utc + "," + lat + "," + lng + "," + mm1 + "," + mmbe + "," + (time - time1) + "," + (time - timebe) + "," + speed + "," + total);
                // 记得换行
                bWriter.newLine();

                latbe = lat;
                lngbe = lng;
                timebe = time;

            }
        }

        bReader.close();
        bWriter.close();
    }

}
